package com.yogee.topseller.adapter;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.MainActivity;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.BeginQueryActivity;
import com.yogee.topseller.activity.CompareProductActivity;
import com.yogee.topseller.activity.CompareProductActivity1;
import com.yogee.topseller.activity.ProductCompareActivity;
import com.yogee.topseller.activity.SearchResultActivity;
import com.yogee.topseller.activity.TagSearchHActivity;
import com.yogee.topseller.activity.login.model.bean.InterestLabelBean;
import com.yogee.topseller.bean.CollectOtherCompareBean;
import com.yogee.topseller.bean.CollectOtherPkBean;
import com.yogee.topseller.bean.InterlligentFilterBean;
import com.yogee.topseller.bean.PKBean;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.bean.SearchResultBean;
import com.yogee.topseller.fragment.collect.CollectBodyOtherFragment;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.GoodBadDialogFragment;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.view.FlowLayoutOther;
import com.yogee.topseller.view.FlowLayoutSearch;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏--搜索，对比，pk适配器
 * Created by Administrator on 2017/7/13.
 */

public class CollectBodyOtherAdapter extends RecyclerView.Adapter<CollectBodyOtherAdapter.CollectOtherViewHolder> {

    Context mContext;
    int collectType = 0; //0.搜索，1.对比，2.pk
    List<CollectOtherPkBean.ProductList> mProductList = new ArrayList<>();//pk数据
    private List<CollectOtherCompareBean.ProductList> compareList = new ArrayList<>();//对比数据
    private List<SearchResultBean.ResultBean> searchList = new ArrayList<>();//对比数据
    //PK的两个数据bean
    PKBean pkBean1, pkBean2;
    Activity mActivity;

    public CollectBodyOtherAdapter(Context context, Activity activity) {
        mContext = context;
        mActivity = activity;
    }

    /**
     * 设置adapter的类别
     *
     * @param type
     */
    public void setCollectType(int type) {
        this.collectType = type;
    }

    /**
     * 设置PK数据
     *
     * @param list
     */
    public void setPkData(List<CollectOtherPkBean.ProductList> list) {
        mProductList = list;
        notifyDataSetChanged();
    }

    /**
     * 设置对比数据
     *
     * @param list
     */
    public void setCompareList(List<CollectOtherCompareBean.ProductList> list) {
        compareList = list;
        this.notifyDataSetChanged();
    }

    /**
     * 设置搜索记录数据
     *
     * @param searchList
     */
    public void setSearchList(List<SearchResultBean.ResultBean> searchList) {
        this.searchList = searchList;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(CollectOtherViewHolder holder, final int position) {
        //搜索
        if (0 == collectType) {
            holder.flowData.setVisibility(View.VISIBLE);
            holder.rightIcon.setVisibility(View.GONE);
            holder.productL.setVisibility(View.GONE);
            holder.tv_more.setVisibility(View.VISIBLE);
            holder.time.setText(searchList.get(position).getDate());
            holder.flowData.cleanTag();
            holder.flowData.setDatas(searchList.get(position).getLabelList());
            holder.detail.setVisibility(View.GONE);
            holder.outLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mContext.startActivity(new Intent(mContext, BeginQueryActivity.class).putExtra("id", searchList.get(position).getQueryId()));
                }
            });

        } else if (1 == collectType) {
            //对比
            holder.flowData.setVisibility(View.GONE);
            holder.productL.setVisibility(View.VISIBLE);
            holder.rightIcon.setVisibility(View.VISIBLE);
            holder.detail.setVisibility(View.GONE);
            holder.tv_more.setVisibility(View.GONE);

            holder.rightIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.vs_list));
            if (compareList.get(position).getDataList().size() >0 ){
                holder.pkName1.setText(compareList.get(position).getDataList().get(0).getName());
                holder.tv_pk_type1.setVisibility(View.VISIBLE);
                holder.tv_pk_type1.setText(compareList.get(position).getDataList().get(0).getProType());
                if (compareList.get(position).getDataList().size()==2){
                    holder.pkName2.setText(compareList.get(position).getDataList().get(1).getName());
                    holder.tv_pk_type2.setVisibility(View.VISIBLE);
                    holder.tv_pk_type2.setText(compareList.get(position).getDataList().get(1).getProType());
                    holder.compareName3.setVisibility(View.GONE);
                    holder.tv_pk_type3.setVisibility(View.GONE);
                }
                //判断是否有第三条数据
                if (compareList.get(position).getDataList().size()==3){
                    holder.compareName3.setText(compareList.get(position).getDataList().get(2).getName());
                    holder.pkName2.setVisibility(View.VISIBLE);
                    holder.tv_pk_type2.setVisibility(View.VISIBLE);
                    holder.tv_pk_type3.setVisibility(View.VISIBLE);
                    holder.compareName3.setVisibility(View.VISIBLE);
                    holder.tv_pk_type3.setText(compareList.get(position).getDataList().get(2).getProType());

                    holder.pkName2.setText(compareList.get(position).getDataList().get(1).getName());
                    holder.tv_pk_type2.setText(compareList.get(position).getDataList().get(1).getProType());
                }
                holder.time.setText(compareList.get(position).getDate());
                holder.outLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CollectOtherCompareBean.ProductList bean = compareList.get(position);
                        String proId = "";

                        if (!bean.getDataList().get(0).getId().equals("0")) {
                            proId += bean.getDataList().get(0).getId();
                        }
                        if (!bean.getDataList().get(1).getId().equals("0")) {
                            proId += "," + bean.getDataList().get(1).getId();
                        }
                        //判断是否有第三条数据
                        if (bean.getDataList().size()>2){
                            if (!bean.getDataList().get(2).getId().equals("0")) {
                                proId += "," + bean.getDataList().get(2).getId();
                            }
                        }
//                        setChooseProId(AppUtil.getUserId(mContext),proId);
                        if(compareList.get(position).getDataList().size()!=3){
                            mContext.startActivity(new Intent(mContext, CompareProductActivity.class).putExtra("num",compareList.get(position).getDataList().size()).putExtra("type", "1").putExtra("proId",proId));

                        }else{
                            mContext.startActivity(new Intent(mContext, CompareProductActivity1.class).putExtra("num",compareList.get(position).getDataList().size()).putExtra("type", "1").putExtra("proId",proId));

                        }
                         }
                });
            }
        } else if (2 == collectType) {
            //PK
            holder.flowData.setVisibility(View.GONE);
            holder.productL.setVisibility(View.VISIBLE);
            holder.rightIcon.setVisibility(View.VISIBLE);
            holder.tv_more.setVisibility(View.GONE);
            holder.rightIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.pk_list));
            if (0 != mProductList.size()) {
                //设置PK产品名称
                holder.pkName1.setText(mProductList.get(position).getDataList().get(0).getProductI());
                holder.tv_pk_type1.setText(mProductList.get(position).getDataList().get(0).getProTypeI());
                holder.tv_pk_type1.setVisibility(View.VISIBLE);

                holder.pkName2.setText(mProductList.get(position).getDataList().get(0).getProductII());
                holder.tv_pk_type2.setText(mProductList.get(position).getDataList().get(0).getProTypeII());
                holder.tv_pk_type2.setVisibility(View.VISIBLE);
                holder.time.setText(mProductList.get(position).getDate());
                final int tempNum = position;
                holder.outLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rebuildPKbean(mProductList.get(tempNum).getDataList().get(0));
                        GoodBadDialogFragment dialogFragment = new GoodBadDialogFragment();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("pk1", pkBean1);
                        bundle.putSerializable("pk2", pkBean2);
                        bundle.putString("nowId", mProductList.get(tempNum).getDataList().get(0).getIdI());
                        bundle.putString("fromCollect", "1");
                        dialogFragment.setArguments(bundle);
                        dialogFragment.show(mActivity.getFragmentManager(), "");
                    }
                });
            }
        }

    }

    private void setChooseProId(String userId, String proId) {
        HttpManager.getInstance().setChooseProId(userId, proId).subscribe(new BaseSubscriber(new HttpOnNextListener<ResultBean>() {
            @Override
            public void onNext(ResultBean o) {
                mContext.startActivity(new Intent(mContext, CompareProductActivity.class).putExtra("type", "1"));
            }
        }, ((MainActivity)mContext)));
    }

    /**
     * 重新组装数据，因为此处的数据bean和
     *
     * @param pkList
     */
    private void rebuildPKbean(CollectOtherPkBean.PkList pkList) {
        pkBean1 = new PKBean(pkList.getIsCollateralizeI(), pkList.getProductI(), pkList.getInvestmentTermI(), pkList.getExpectReturnI(), pkList.getCommissionI(),pkList.getIdI(),pkList.getProTypeI());
        pkBean2 = new PKBean(pkList.getIsCollateralizeII(), pkList.getProductII(), pkList.getInvestmentTermII(), pkList.getExpectReturnII(), pkList.getCommissionII(),pkList.getIdII(),pkList.getProTypeII());
    }

    @Override
    public CollectOtherViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.item_collect_body_other, null);
        return new CollectOtherViewHolder(contentView);
    }

    @Override
    public int getItemCount() {
        if (2 == collectType) {
            return null == mProductList ? 0 : mProductList.size();
        } else if (1 == collectType) {
            return null == compareList ? 0 : compareList.size();
        } else if (0 == collectType) {
            return null == searchList ? 0 : searchList.size();
        }
        return 0;
    }

    class CollectOtherViewHolder extends RecyclerView.ViewHolder {

        private TextView time, detail,tv_more;
        private ImageView rightIcon;
        private FlowLayoutSearch flowData;
        private LinearLayout productL, outLayout;
        private TextView pkName1, pkName2, compareName3;

        private TextView tv_pk_type1,tv_pk_type2,tv_pk_type3;

        public CollectOtherViewHolder(View view) {
            super(view);
            rightIcon = (ImageView) view.findViewById(R.id.iv_flag);
            flowData = (FlowLayoutSearch) view.findViewById(R.id.fl_collect_label_data);
            productL = (LinearLayout) view.findViewById(R.id.ll_pk_product_list);
            pkName1 = (TextView) view.findViewById(R.id.tv_pk_name1);
            pkName2 = (TextView) view.findViewById(R.id.tv_pk_name2);
            compareName3 = (TextView) view.findViewById(R.id.tv_compare_name3);
            time = (TextView) view.findViewById(R.id.tv_time);
            detail = (TextView) view.findViewById(R.id.tv_detail);
            outLayout = (LinearLayout) view.findViewById(R.id.collect_other_bg);

            tv_pk_type1 = (TextView) view.findViewById(R.id.tv_pk_type1);
            tv_pk_type2 = (TextView) view.findViewById(R.id.tv_pk_type2);
            tv_pk_type3 = (TextView) view.findViewById(R.id.tv_pk_type3);
            tv_more = (TextView) view.findViewById(R.id.tv_more);
        }
    }
}
